﻿using System.Windows;
using Red.Controls.DataDriven;
using Red.Controls.Shell;
using Red.Controls.ViewModels;

namespace Red.Controls.Curve.DataDriven
{
    /// <summary>
    /// Interaction logic for CurveControlDropDown.xaml
    /// </summary>
    [InteropControl( "CurveData" )]
    public partial class CurveControlDropDown : DropDown
    {
        public CurveControlDropDown()
        {
            InitializeComponent();
        }

        protected override void OnPropertyChanged( DependencyPropertyChangedEventArgs e )
        {
            base.OnPropertyChanged( e );

            if ( e.Property == DataContextProperty )
            {
                var dataItem = e.NewValue as DataItem;
                if ( dataItem != null )
                {
                    _editor.DataContext = dataItem;
                }
            }
        }

        private void DropDown_PanelClosed( object sender, DropDown.PanelClosedEventArgs e )
        {
            var dataItem = DataContext as DataItem;
            if ( dataItem != null )
            {
                if ( e.Result )
                {
                    _editor.Save();
                }
                else
                {
                    _editor.RevertChanges();
                }
            }
        }
    }
}
